﻿using SYMetro.DataObjects;
using SYMetro.ServiceContract.Membership;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace SYMetro.Web.MVC.App_Code
{
    public class MetroRoleProvider : RoleProvider
    {
        public override string ApplicationName
        {
            get;
            set;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                proxy.Channel.AddUsersToRoles(usernames, roleNames);
            }
        }

        public override void CreateRole(string roleName)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                RoleDataObject role = new RoleDataObject() { RoleName = roleName };
                RoleDataObjectList list = new RoleDataObjectList();
                list.Add(role);
                proxy.Channel.CreateRoles(list);
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {                
                return proxy.Channel.DeleteRole(roleName, throwOnPopulatedRole);
            }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                var lst = proxy.Channel.FindUsersInRole(roleName, usernameToMatch);
                return lst.Select(u => u.UserName).ToArray();
            }
        }

        public override string[] GetAllRoles()
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                var lst = proxy.Channel.GetAllRoles();
                return lst.Select(r => r.RoleName).ToArray();
            }
        }

        public override string[] GetRolesForUser(string username)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                var lst = proxy.Channel.GetRolesForUser(username);
                return lst.Select(r => r.RoleName).ToArray();
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                var lst = proxy.Channel.GetUsersInRole(roleName);
                return lst.Select(u => u.UserName).ToArray();
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                return proxy.Channel.IsUserInRole(username,roleName);                
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                proxy.Channel.RemoveUsersFromRoles(usernames, roleNames);
            }
        }

        public override bool RoleExists(string roleName)
        {
            using (var proxy = new WebServiceProxy<IRoleService>())
            {
                return proxy.Channel.RoleExists(roleName);
            }
        }
    }
}